#!/usr/bin/env python3
import numpy as np

samples_and = [
    [0, 0, 0],
    [1, 0, 0],
    [0, 1, 0],
    [1, 1, 1],
]

samples_or = [
    [0, 0, 0],
    [1, 0, 1],
    [0, 1, 1],
    [1, 1, 1],
]

samples_xor = [
    [0, 0, 0],
    [1, 0, 1],
    [0, 1, 1],
    [1, 1, 0],
]


def perceptron(samples):
    # 初始化权重参数
    w = np.array([1, 2])
    b = 0
    a = 1

    for i in range(10):
        for j in range(4):
            # 输入数据x
            x = np.array(samples[j][:2])
            # 输出数据y
            y = 1 if np.dot(w, x) + b > 0 else 0
            # 期望值d
            d = np.array(samples[j][2])
            # 计算权重梯度
            delta_b = a * (d - y)
            delta_w = a * (d - y) * x
            # 计算损失值
            cost = 0.5*(d-y)**2
            print('epoch {} sample {} loss {}  [{} {} {} {} {} {} {}]'.format(
                i, j, cost, w[0], w[1], b, y, delta_w[0], delta_w[1], delta_b
            ))
            # 权重更新
            w = w + delta_w
            b = b + delta_b


if __name__ == '__main__':
    print('logical and')
    perceptron(samples_and)
    print('logical or')
    perceptron(samples_or)
    print('logical xor')
    perceptron(samples_xor)
